package com.three.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.entity.Outlibrary;
import com.three.entity.Staff;
import com.three.mapper.OutlibraryGoodsMapper;
import com.three.mapper.OutlibraryMapper;
import com.three.service.OutLibraryInformationService;
import com.three.vo.OutGoodsVo;
import com.three.vo.OutLibraryInformationVo;
import com.three.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class OutLibraryInformationServiceImpl implements OutLibraryInformationService {
    @Autowired
    private OutlibraryMapper outlibraryMapper;
    @Autowired
    private HttpSession session;

    @Autowired
    private OutlibraryGoodsMapper outlibraryGoodsMapper;
    @Override
    public R getOutLibraryInformation(Integer currentPage, Integer pageSize, OutLibraryInformationVo outLibraryInformationVo) {
        //开启分页
        PageHelper.startPage(currentPage,pageSize);
        List<Outlibrary> allOutLibraryEx = outlibraryMapper.getOutLibraryInformation(outLibraryInformationVo);
        //封装
        PageInfo<Outlibrary> pageInfo = new PageInfo<>(allOutLibraryEx);
        return R.ok(pageInfo);
    }

    @Override
    //添加出库信息（零售）
    public R addOutLibraryInformation(OutGoodsVo outGoodsVo) {
        //为outlibrary赋值，添加outlibrary
        Outlibrary outlibrary = new Outlibrary();
        Staff staff = (Staff) session.getAttribute("staff");
        outlibrary.setOutstaffid(staff.getStaffid());
        outlibrary.setOuttime(new Date());
        outlibrary.setExamstaffid(staff.getStaffid());
        outlibrary.setExamtime(new Date());
        outlibrary.setOutmethod(1);
        outlibrary.setExamstate(2);
        outlibrary.setBranchid(staff.getBranchid());
        int insert = outlibraryMapper.insertSelective(outlibrary);
        //为outGoodsVo赋值，添加outGoods
        if(insert>0){
            outGoodsVo.setCid(outlibrary.getCid());
            outGoodsVo.setTotal(outGoodsVo.getSprice()*outGoodsVo.getNum());
            int i = outlibraryGoodsMapper.insertOutGoods(outGoodsVo);
            if (i>0){
                return R.ok();
            }
        }
        return R.ok();
    }

}
